package tank;

import gameBasics.GameModel;
import gameBasics.GameObject;
import gameBasics.ResourceManager;

import java.awt.*;

public class MainTank extends Tank{

    //有参构造方法
    public MainTank(TankType tankType, int x, int y, Dir dir) {
        super(tankType, x, y, dir);
    }

    /**
     * 绘制主坦克
     * @param graphics 画笔对象
     */
    @Override
    public void paint(Graphics graphics) {
        if(!livingState) GameModel.getInstance().removeGameObject(this);

        //为主坦克添加姓名
        Color color = graphics.getColor();
        graphics.setColor(Color.lightGray);
        graphics.drawString("player",x + WIDTH / 6,y - 5);
        graphics.setColor(color);

        switch (dir){
            case LEFT -> graphics.drawImage(ResourceManager.tankL, x, y, null);
            case UP -> graphics.drawImage(ResourceManager.tankU, x, y, null);
            case RIGHT -> graphics.drawImage(ResourceManager.tankR, x, y, null);
            case DOWN -> graphics.drawImage(ResourceManager.tankD, x, y, null);
        }

        move();
    }

    /**
     * 坦克与游戏物品的碰撞检测
     */
    public void gameObjectCheck(GameObject gameObject){
        Rectangle rectangle;

        switch (dir){
            case LEFT -> {
                rectangle = new Rectangle(x - 1, y, WIDTH, HEIGHT);
                if(gameObject.getRectangle().intersects(rectangle)){
                    x += SPEED;
                }
            }
            case UP -> {
                rectangle = new Rectangle(x, y - 1, WIDTH, HEIGHT);
                if(gameObject.getRectangle().intersects(rectangle)){
                    y += SPEED;
                }
            }
            case RIGHT -> {
                rectangle = new Rectangle(x + 1, y, WIDTH, HEIGHT);
                if(gameObject.getRectangle().intersects(rectangle)){
                    x -= SPEED;
                }
            }
            case DOWN -> {
                rectangle = new Rectangle(x , y + 1, WIDTH, HEIGHT);
                if(gameObject.getRectangle().intersects(rectangle)){
                    y -= SPEED;
                }
            }
        }
    }

}
